<?php


namespace App\Http\Controllers\ProductCenter;

use App\Exceptions\ValidatorException;
use App\Http\Controllers\Controller;
use App\Http\Repositories\Repository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class SpuController extends Controller
{
    private $repository;
    public function __construct(Repository $repository)
    {
        $this->repository = $repository;
    }

    /**
     * 添加商品
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function addSpu(Request $request)
    {
        //验证规则
        $rules = [
            'name' => 'required|string',
            'en_name' => 'required|string',
            'category_id' => 'required|int',
            'brand_id' => 'required|int',
            'image_url' => 'required|string',
            'attribute' => 'nullable|array',
            'describe' => 'nullable|string',

            'sku' => 'nullable|array',
            'sku.*.sku' => 'required_with:sku|string',
            'sku.*.name' => 'required_with:sku|string',
            'sku.*.image_url' => 'nullable|string',
            'sku.*.price' => 'nullable|numeric',
            'sku.*.newest_price' => 'nullable|numeric',
            'sku.*.stock' => 'nullable|numeric',
//            'sku.*.describe' => 'required_with:sku|string',

            'sku.*.sku_info' => 'nullable|array',
            'sku.*.sku_info.*.id' => 'required_with:sku.*.sku_info|numeric',
//            'sku.*.sku_info.*.stock' => 'required_with:sku.*.sku_info|numeric',
//            'sku.*.sku_info.*.price' => 'required_with:sku|numeric',
//            'sku.*.sku_info.*.newest_price' => 'required_with:sku|numeric',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->addSpu($validator->getData());
    }

    /**
     * 删除
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function delSpu(Request $request)
    {
        //验证规则
        $rules = [
            'ids' => 'required'
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->delSpu($validator->getData());
    }

    /**
     * 编辑
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function editSpu(Request $request)
    {
        //验证规则
        $rules = [
            'id' => 'required|int',
            'name' => 'required|string',
            'en_name' => 'required|string',
            'category_id' => 'required|int',
            'brand_id' => 'required|int',
            'image_url' => 'required|string',
            'attribute' => 'nullable|array',
            'describe' => 'nullable|string',

            'sku' => 'nullable|array',
            'sku.*.sku' => 'required_with:sku|string',
            'sku.*.name' => 'required_with:sku|string',
            'sku.*.image_url' => 'nullable|string',
            'sku.*.price' => 'nullable|numeric',
            'sku.*.newest_price' => 'nullable|numeric',
            'sku.*.stock' => 'nullable|numeric',
//            'sku.*.describe' => 'required_with:sku|string',

            'sku.*.sku_info' => 'nullable|array',
            'sku.*.sku_info.*.id' => 'required_with:sku.*.sku_info|numeric',
//            'sku.*.sku_info.*.stock' => 'required_with:sku.*.sku_info|numeric',
//            'sku.*.sku_info.*.price' => 'required_with:sku|numeric',
//            'sku.*.sku_info.*.newest_price' => 'required_with:sku|numeric',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->editSpu($validator->getData());
    }

    /**
     * 获取列表
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function getSpuList(Request $request)
    {
        //验证规则
        $rules = [
            'name' => 'nullable|string',
            'spu' => 'nullable|string',
            'category_id' => 'nullable|int',
            'create_user_uuid' => 'nullable|string',
            'brand_id' => 'nullable',
            'create_time' => 'nullable',
            'status' => 'nullable|int',
            'sku' => 'nullable|string',
            'price' => 'nullable',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->getSpuList($validator->getData());
    }

    /**
     * 获取详情
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function getSpuInfo(Request $request)
    {
        //验证规则
        $rules = [
            'id' => 'required_without:spu|int',
            'spu' => 'required_without:id|string',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->getSpuInfo($validator->getData());
    }

    /**
     * 修改 status 状态值
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function editSpuStatus(Request $request)
    {
        //验证规则
        $rules = [
            'id' => 'required',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->editSpuStatus($validator->getData());
    }

    public function getProductListV1(Request $request)
    {
        //验证规则
        $rules = [
            'name' => 'nullable|string',
            'category_id' => 'nullable|int',
            'brand_id' => 'nullable',
            'sku' => 'nullable|string',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->getProductListV1($validator->getData());
    }

    public function getProductInfoV1(Request $request)
    {
        //验证规则
        $rules = [
            'id' => 'required|int'
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->getProductInfoV1($validator->getData());
    }
}
